package ${config.rootPackage};

import com.gwssi.optimus.core.exception.OptimusException;
import com.gwssi.optimus.core.persistence.dao.IPersistenceDAO;
import com.gwssi.optimus.core.service.BaseService;
import com.gwssi.optimus.core.util.SqlHelper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * @Description ${table.tableComment}的业务service
 * @Date ${(.now)?string("yyyy-MM-dd")}
 */
@Service
public class ${table.controllerPrefix}Service extends BaseService {

    private static Logger logger = LoggerFactory.getLogger(${table.controllerPrefix}Service.class);

    /**
     * 新增或编辑${table.tableComment!}
     * @param ${table.attrPrefix}BO 数据域对象
     * @return ${table.tableComment!}Id
     * @throws OptimusException
     */
    public String saveOrUpdate(${table.boPrefix}BO ${table.attrPrefix}BO) throws OptimusException{
        IPersistenceDAO dao = getPersistenceDAO();
        duplicateNameCheck(${table.attrPrefix}BO.get${table.controllerPrefix}Id(), ${table.attrPrefix}BO.get${table.controllerPrefix}Name(), ${table.attrPrefix}BO.get${table.controllerPrefix}TypeId());
        if(StringUtils.isBlank(${table.attrPrefix}BO.get${table.controllerPrefix}Id())){
            dao.insert(${table.attrPrefix}BO);
        }else{
            dao.update(${table.attrPrefix}BO);
        }
        return ${table.attrPrefix}BO.get${table.controllerPrefix}Id();
    }

    /**
     *  重名校验 @TODO 需要修改
     * @param id 保存对象的id
     * @param name 保存对象的名称
     * @param typeId 保存对象所属类型的id
     * @throws OptimusException
     */
    private void duplicateNameCheck(String id, String name, String typeId) throws OptimusException {
        IPersistenceDAO dao = DAOManager.getPersistenceDAO();
        SqlHelper sqlHelper = SqlHelper.newInstance();
        sqlHelper.append("select count(1) from ${table.tableName} WHERE NAME = ? AND TYPE_ID = ? ", name, typeId)
                .appendIfNotBlank("AND ID != ? ", id);
        int i = dao.queryForInt(sqlHelper.getSql(), sqlHelper.getParamList());
        if (i > 0) {
            throw new OptimusException("${table.tableComment!}名称已存在！");
        }
    }

    /**
     * 根据${table.tableComment!}IDs的查询sql删除
     * @param ids ${table.tableComment!}IDs的查询sql
     * @throws OptimusException
     */
    public void deleteBySql(SqlHelper idSql) throws OptimusException {
        SqlHelper childIdSql = SqlHelper.newInstance();
        cardIdSql.append("select child_id from ${table.tableName}_CHILD where ID in ( ").append(idSql).append(")");
        childService.deleteBySql(cardIdSql);

        SqlHelper deleteSql = SqlHelper.newInstance();
        deleteSql.append("delete from ${table.tableName} where ${table.columnPrefix}_ID in ( ").append(idSql).append(")");
        IPersistenceDAO dao = getPersistenceDAO();
        dao.execute(deleteSql.getSql(), deleteSql.getParamList());
    }

    /**
     * 根据${table.tableComment!}的IDs删除
     * @param ids ${table.tableComment!}的IDs
     * @throws OptimusException
     */
    public void deleteByKeys(List<String> ids)throws OptimusException {
        if (ids == null || ids.isEmpty()) {
        return;
        }
        SqlHelper idsSql = SqlHelper.newInstance();
        ids.forEach(id -> idsSql.append("?,", id));
        idsSql.setLength(idsSql.length() - 1);
        deleteBySql(idsSql);
    }

    /**
     * 根据${table.tableComment!}的ID删除
     * @param id ${table.tableComment!}的ID
     * @throws OptimusException
     */
    public void deleteByKey(String id)throws OptimusException {
        deleteByKeys(Collections.singletonList(id));
    }

    /**
     * 根据${table.tableComment!}的ID查询详情
     * @param id ${table.tableComment!}的ID
     * @throws OptimusException
     */
    public ${table.boPrefix}BO queryByKey(String id)throws OptimusException {
        IPersistenceDAO dao = getPersistenceDAO();
        return dao.queryByKey(${table.boPrefix}BO.class, id);
    }

    /**
     * 根据查询条件，查询列表，支持分页
     * @param id ${table.tableComment!}的ID
     * @param name ${table.tableComment!}的英文名
     * @param label ${table.tableComment!}的中文名
     * @throws OptimusException
     */
    public List<${table.boPrefix}BO> queryList(String id, String name, String label)throws OptimusException {
        SqlHelper sql = SqlHelper.newInstance();
        sql.append("select * from ${table.tableName} where 1=1 ");
        sql.appendIfNotBlank(" and ${table.columnPrefix}_ID = ? ", id);
        sql.appendLikeIfNotBlank(" and ${table.columnPrefix}_NAME like ? ", name);
        sql.appendLikeIfNotBlank(" and ${table.columnPrefix}_LABEL like ? ", label);
        IPersistenceDAO dao = getPersistenceDAO();
        return dao.pageQueryForList(${table.boPrefix}BO.class, sql.getSql(), sql.getParamList());
    }

}
